{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "47f818ba-fe67-401c-b913-db13233d778b",
   "metadata": {},
   "source": [
    "# 任务要求\n",
    "### 调用API实现Svm，预测哪些乘客被异常运送。\n",
    "### 将Svm封装为函数Svm()。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "595c9b54-cc2f-400d-8d7f-cc87a8a55fb2",
   "metadata": {},
   "source": [
    "### 导入必要的包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1ca88d79-5eba-4b61-9821-7b5e2399a1e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.svm import SVC\n",
    "from sklearn.feature_selection import RFECV\n",
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb46200e-15af-4d75-9101-29e08e6a724c",
   "metadata": {},
   "source": [
    "### 训练 `Svm` 并保存\n",
    "\n",
    "* 读取特征选择后的数据集，路径为 `FeatureSelectedData/svm_train.csv`\n",
    "* 保存路径为 `/model/svm_model.pkl`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22ac4fc3-9e3b-4633-8540-4db706df4ed6",
   "metadata": {},
   "source": [
    "### 导入保存/加载模型用的包： `joblib` 中的 `dump`, `load`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "35d738a7-ab6b-4de8-9c1e-3eae752761d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "from joblib import dump, load"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9e99712-6341-4f6b-94a9-188949544c4e",
   "metadata": {},
   "source": [
    "### 加载数据集并训练 Svm 分类器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "fe4a8797-a4f3-4838-8788-6c496702bf33",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model saved as svm_model.joblib\n"
     ]
    }
   ],
   "source": [
    "final_train_data = pd.read_csv('../../FeatureSelectedData/selected_train.csv')\n",
    "final_X_train = final_train_data.iloc[:, :-1]\n",
    "final_y_train = final_train_data.iloc[:, -1]\n",
    "\n",
    "clf = SVC(kernel='linear', C=1)\n",
    "clf.fit(final_X_train, final_y_train)\n",
    "\n",
    "dump(clf, '../../ModelFile/Svm/svm_model.joblib')\n",
    "print(\"Model saved as svm_model.joblib\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6a07eba-91b6-448f-a440-0c781c70c9d9",
   "metadata": {},
   "source": [
    "### 将 `Svm` 模型封装为 `Svm()` 函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f7c4a7d7-2c75-4d89-9cb7-87bd26bf8403",
   "metadata": {},
   "outputs": [],
   "source": [
    "def Svm(test_data):\n",
    "    clf = load('../../ModelFile/Svm/svm_model.joblib')\n",
    "    y_predicted = clf.predict(test_data)\n",
    "    return y_predicted"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c93882da-de61-47df-bac3-4da2874e7dfd",
   "metadata": {},
   "source": [
    "### 调用 `Svm()` 函数对测试集进行预测\n",
    "将预测结果输出为 `result_svm.csv`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c77d84bd-f576-41bc-9f21-d5ed1e77daed",
   "metadata": {},
   "outputs": [],
   "source": [
    "final_test_data = pd.read_csv('../../FeatureSelectedData/selected_test.csv')\n",
    "result_file = pd.read_csv('../../RawData/sample_submission.csv')\n",
    "predictions = Svm(final_test_data)\n",
    "predictions_bool = (predictions == 1)\n",
    "result_file[\"Transported\"] = pd.Series(predictions_bool, result_file.index)\n",
    "result_file.to_csv('../../predictions/Svm/result_svm.csv', index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d90bfcc-c500-4614-9473-a4ae654ac173",
   "metadata": {},
   "source": [
    "### 将 `result_svm.csv` 上传到 `kaggle` 上评分"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
